﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace X9Tech.HashWall
{
	/// <summary>
	/// Interaction logic for ContentLineItemControl.xaml
	/// </summary>
	public partial class ContentLineItemControl
	{
		public ContentLineItemControl()
		{
			this.InitializeComponent();
		}

        public void LoadMessage(Core.Models.WallMessage message)
        {
            this.UserDisplayName.Content = (!string.IsNullOrEmpty(message.UserDisplayName)) ? message.UserDisplayName : "Unknown User";
            this.MessageSubtextLabel.Content = (!string.IsNullOrEmpty(message.Subtext)) ? message.Subtext : string.Empty;
            this.MessageTextLabel.Text = (!string.IsNullOrEmpty(message.Text)) ? message.Text : string.Empty;

            if (message.UserImage != null)
            {
                ImageSource iSrc = ConvertBitmap(new System.Drawing.Bitmap(message.UserImage));

                this.MessageImage.Source = iSrc;
            }
            else
            {
                this.MessageImage.Source = null;
            }

            this.MessageSubtextLabel.Content = message.MessageDateTime.ToLongDateString() + " " + message.MessageDateTime.ToShortTimeString();
                    }

        public static BitmapSource ConvertBitmap(System.Drawing.Bitmap source)
        {
            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(source.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
        }

        public void Collapse()
        {
            this.Height = 0;
            //Storyboard sbCollapse = (Storyboard)this.FindResource("ShrinkToNothing");
            //this.BeginStoryboard(sbCollapse);
        }

        public void Expand()
        {
            Storyboard storyboard = new Storyboard();
            TimeSpan duration = new TimeSpan(0, 0, 1);

            // Create a DoubleAnimation to fade the not selected option control
            DoubleAnimation animation = new DoubleAnimation();

            animation.From = 0.0;
            animation.To = 132;
            animation.Duration = new Duration(duration);
            // Configure the animation to target de property Opacity
            Storyboard.SetTargetName(animation, this.Name);
            Storyboard.SetTargetProperty(animation, new PropertyPath(Control.HeightProperty));
            // Add the animation to the storyboard
            storyboard.Children.Add(animation);

            // Begin the storyboard
            storyboard.Begin(this);
        }


	}
}